package org.klaoye.musicplayer.bean;

import android.content.Context;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.klaoye.musicplayer.R;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FileSelector {
    final Context context;//上下文
    private File basePath;//根文件夹
    File[] files;//文件数组
    ArrayList<String> fileNames;//文件名数列
    ListView mainView;//主要列表视图
    ArrayList<Map<String, Object>> folderArrayList;//为adapter适配的数组列表
    SimpleAdapter simpleAdapter;//数据adapter

    public FileSelector(Context context, ListView listview, File file) {
        this.context = context;
        this.mainView = listview;
        if (fileIsDirectory(file)) {
            basePath = file;
        }
        createAdapter();
    }

    public FileSelector(Context context, File file) {
        this.context = context;
        if (fileIsDirectory(file)) {
            basePath = file;
        }
    }

    public FileSelector(Context context) {
        this.context = context;
    }

    public void setListView(ListView listView) {
        this.mainView = listView;
        createAdapter();
    }

    public void setBasePath(File file) {
        if (fileIsDirectory(file)) {
            basePath = file;
        }
    }

    public File[] listFile(File[] parentFiles) {
        folderArrayList.clear();
        fileNames = new ArrayList<>();
        files = parentFiles;
        for (File f : parentFiles) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", f.getName());
            map.put("img", (f.isDirectory()) ? R.drawable.folder : R.drawable.music);
            fileNames.add(f.getName());
            folderArrayList.add(map);
        }
        viewChanged();
        return files;
    }

    public File[] listFile(File parent, FileFilter filter) {
        if (parent.isFile()) return new File[0];//文件路径直接返回
        if (filter != null) {//如果没有筛选需要，filter直接传入空值
            files = parent.listFiles(filter);
        } else {
            files = parent.listFiles();
        }
        if (files == null) return null;
        folderArrayList.clear();
        fileNames = new ArrayList<>();
        for (File f : files) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", f.getName());
            map.put("img", (f.isDirectory()) ? R.drawable.folder : R.drawable.music);
            fileNames.add(f.getName());
            folderArrayList.add(map);
        }
        viewChanged();
        return files;
    }

    private void viewChanged() {
        mainView.removeAllViewsInLayout();
        simpleAdapter.notifyDataSetChanged();
        simpleAdapter.notifyDataSetInvalidated();
        if (!mainView.getAdapter().equals(simpleAdapter)) {//改变适配器
            mainView.setAdapter(simpleAdapter);
        }
        Log.i("ListViewFiles", Arrays.toString(files));
    }

    private void createAdapter() {//adapter初始化
        folderArrayList = new ArrayList<>();
        simpleAdapter = new SimpleAdapter(
                context,
                folderArrayList,
                R.layout.folder_item,
                new String[]{"name", "img"},
                new int[]{R.id.textView_item, R.id.imageView_item});
        mainView.setAdapter(simpleAdapter);
    }

    private boolean fileIsDirectory(File file) {//判断这个文件是否为文件夹
        if (file.exists()) {
            if (!file.isDirectory()) {
                Log.e("", "This file not a directory,please gave a directory path!");
                return false;
            }
        } else {
            //throw new Exception("This file is not exist!");
            Log.e("", "This file is not exist!");
            return false;
        }
        return true;
    }

}
